package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.StorageDao;
import com.xueyu.domain.Products.ProductsBean;
import com.xueyu.domain.Storage.StorageBean;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/storage")
public class StorageController {
    @Autowired
    StorageDao storageDao;

    @RequestMapping("/add")
    public ResultBean add(@RequestBody StorageBean StorageBean) throws Exception{
        ResultBean ResultBean= new ResultBean();
        if (StorageBean.getStorageId()==0){
            storageDao.insert(StorageBean);
        }else {
            storageDao.updateById(StorageBean);
        }
        return ResultBean;
    }
    @RequestMapping("/del/{storageId}")
    public ResultBean del(@PathVariable("storageId") int storageId){
        storageDao.deleteById(storageId);
        return new ResultBean();
    }
    @RequestMapping("/getid/{storageId}")
    public Map<String, Object> getid(@PathVariable("storageId") int storageId){
        ResultBean resultBean = new ResultBean();
        if (storageId>0){
            QueryWrapper<StorageBean> queryWrapper = Wrappers.query();
            queryWrapper.eq("storageId",storageId);
            List<StorageBean> StorageList = storageDao.selectList(queryWrapper);
            resultBean.put("data",StorageList);
        }
        return resultBean;
    }
    @RequestMapping("/fy")
    public ResultBean fy(@RequestBody Map map){
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        Page<StorageBean> page = new Page<>(current, size);
        QueryWrapper<StorageBean> queryWrapper = createSchoolQueryWrapper(map);
        page = storageDao.selectPage(page,queryWrapper);
        List<StorageBean> list = page.getRecords();
        ResultBean resultBean = new ResultBean();
        resultBean.put("page",page);
        resultBean.put("list",list);
        return resultBean;
    }
    @RequestMapping("/zd")
    public ResultBean zd(@RequestBody Map map){
        QueryWrapper<StorageBean> queryWrapper =  createSchoolQueryWrapper(map);
        List<StorageBean> list = storageDao.selectList(queryWrapper);
        ResultBean resultBean = new ResultBean();
        resultBean.put("list",list);
        return resultBean;
    }
    private QueryWrapper<StorageBean> createSchoolQueryWrapper(Map map) {
        String storageId = (String) map.get("storageId");
        String storageName = (String) map.get("storageName");
        QueryWrapper<StorageBean> queryWrapper = Wrappers.query();
        if (storageId != null && !"".equals(storageId)) {
            queryWrapper.eq("storageId", storageId);
        }
        if (storageName != null && !"".equals(storageName)) {
            queryWrapper.like("storageName", "%" + storageName + "%");
        }
        return queryWrapper;
    }
}
